#!/bin/bash

#---------------------------------------------------------------------------------
# directory and filename which records the backup actions.
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/schedOps.log
   
LOG_ERROR_LOG=/tmp/schedOps.log

# parameter 1, unique identifier located 2 times ONLY in crontab
# file, once in comment line, other instance in cron data line
JOBID=$1

# plan ahead a bit. Future work will most likely incorporate
# more scheduled tasks than just a 'backup'
TASK_ID=1

# script to execute when the scheduled operation event occurrs
CMD=/opt/hsc/bin/backuphdr


# common point to invoke the logging code
logevent() {
   # set up environment
   SAVED_CLASSPATH=$CLASSPATH
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

   CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/xerces.jar:/usr/websm/codebase/pluginjars/HwmcaCommon.jar:/usr/websm/codebase/pluginjars/auifw.jar:$CLASSPATH
   export CLASSPATH

   JAVAPATH="/opt/IBMJava2-131/jre/bin/"
   x=`type -p java 2>/dev/null`
   if [ "$x" != "" ]
   then
     JAVAPATH=`/usr/bin/dirname $x`
   fi
   export PATH=$JAVAPATH:$PATH
#   export LD_LIBRARY_PATH=/opt/hsc/lib/hmcjni:$LD_LIBRARY_PATH
   export LD_LIBRARY_PATH=/usr/lib:$LD_LIBRARY_PATH

   # call actual program that does the logging
   java com.ibm.hsc.common.util.LogShellEvent ${1} ${2}

   # restore the environment
   CLASSPATH=SAVED_CLASSPATH
   export CLASSPATH
}  


# Check if the directory for the log file exists.
if [ ! -d $LOGDIR ]; then
	echo "=================================================================" > $LOG_ERROR_LOG
	echo -e "Scheduled Operation task log for `date`." >> $LOG_ERROR_LOG
	echo "Scheduled Operation task log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
	exit 1
fi

# Start log to record scheduled op actions.
echo -e "Scheduled Operations log for `date`.\n" > $LOG


#
# Simply execute the (backup critical console data) command - no parameters
#
echo -e "command to execute is $CMD." >> $LOG

$CMD
RC=$?

echo -e "$CMD completed, return value = $RC.\n" >> $LOG

# Log the event
logevent $RC $TASK_ID


#
# remove this job from the crontab file if it is a non-repeating event
#
# if the "repeatFlag" in the crontab comment line indicates a
# non-repeating event (;0;), re-write the crontab file without
# ths entry
#

# cron "query" command
CRONTAB=/usr/bin/crontab

# STDOUT directed to /tmp/crontab_output
CRONTAB_OUTPUT=/tmp/crontab_output

# query the current list of scheduled ops
$CRONTAB -l > $CRONTAB_OUTPUT

BZ=`grep $JOBID $CRONTAB_OUTPUT | grep -v ";0;" | wc -l`

if [ $BZ = 1 ]
then
       CRONTAB_NEW=/tmp/crontab_new
       grep -v $JOBID $CRONTAB_OUTPUT | grep -v "DO NOT EDIT" | grep -v "(" > $CRONTAB_NEW && \
       $CRONTAB -u root $CRONTAB_NEW
       rm -f $CRONTAB_NEW
       
       echo -e "jobID <$JOBID> was removed from the crontab.\n" >> $LOG
       echo -e "Scheduled Operation task completed." >> $LOG
       exit 0
else
       echo -e "jobID <$JOBID> not purged from the crontab.\n" >> $LOG
       echo -e "Scheduled Operation task completed." >> $LOG
       exit 0
fi
